package com.mapapp.flutter_mapapp.widget;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.TextView;

import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.util.ToastUtils;

import razerdp.basepopup.BasePopupWindow;

public class UploadDataConfirmDialog extends BasePopupWindow {

    public TextView tvCompleted;
    public TextView tvUncompleted;
    private CardView cvSubmit;
    private CardView cvSave;
    private CardView cvCancel;
    private boolean hideOptions;


    public UploadDataConfirmDialog(Context context) {
        this(context, false);
    }

    public UploadDataConfirmDialog(Context context, boolean hideOptions) {
        super(context, true);
        this.hideOptions = hideOptions;
        delayInit();
        init();
    }

    @Override
    public View onCreateContentView() {
        View view = createPopupById(R.layout.dialog_upload_data_confirm);
        tvCompleted = view.findViewById(R.id.tv_completed);
        tvUncompleted = view.findViewById(R.id.tv_uncompleted);
        cvSubmit = view.findViewById(R.id.cv_submit);
        cvSave = view.findViewById(R.id.cv_save);
        cvCancel = view.findViewById(R.id.cv_cancel);
        if (hideOptions) {
            tvCompleted.setVisibility(View.GONE);
            tvUncompleted.setVisibility(View.GONE);
        }
        return view;
    }

    private void init() {
        tvCompleted.setSelected(true);
        tvCompleted.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvCompleted.setSelected(true);
                tvUncompleted.setSelected(false);
            }
        });

        tvUncompleted.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvCompleted.setSelected(false);
                tvUncompleted.setSelected(true);
            }
        });

        cvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!tvCompleted.isSelected() && !tvUncompleted.isSelected()) {
                    ToastUtils.showMessage(getContext(), "请确定是否已完成");
                    return;
                }
                if (listener != null) {
                    listener.onClickSubmit(getState());
                }
                dismiss();
            }
        });

        cvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!tvCompleted.isSelected() && !tvUncompleted.isSelected()) {
                    ToastUtils.showMessage(getContext(), "请确定是否已完成");
                    return;
                }
                if (listener != null) {
                    listener.onClickSave(getState());
                }
                dismiss();
            }
        });

        cvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    private int getState() {
        return tvCompleted.isSelected() ? 1 : -1;
    }


    public interface OnItemClickListener {
        void onClickSubmit(int state);

        void onClickSave(int state);
    }

    private OnItemClickListener listener;

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
